<?php 

// Namespace

namespace Loop;


/**
 * CObjectManager allows to manipulate objects
 */

class CObjectManager 
{
	// Properties

	private $sharedObjects = null;


	/**
	 * Constructs an object manager
	 */

	public function __construct()
	{
		$this->sharedObjects = array();
	}


	/**
	 * Get the creation history of given objects
	 *
	 * @param 	array 	$objects 	The array of objects that we want to get history
	 * @param 	int 	$offset 	The offset of the next result to return
	 * @param 	array 	$userIds 	The array of selected users Ids
	 *
	 * @required $objects
	 *
	 * @default $offset=null
	 *
	 * @return array 	The array which contains the objects creation history
	 */

	public function api_getHistory()
	{
		// Get API arguments

		$objects = fv('objects', true);
		$offset = fv('offset', false);
		$userIds = fv('userIds', false);


		// Call the method

		$result = $this->getHistory($userIds, $objects, $offset);


		return $result;
	}
	
	
	/**
	 * Returns the creation history of given objects
	 *
	 * @param 	array 	$userIds 	The array of selected users Ids
	 * @param 	array 	$objects 	The array of objects that we want to get history
	 * @param 	int 	$offset 	The offset of the next result to return
	 *
	 * @return 	array 	The array which contains the objects creation history
	 */

	public function getHistory($userIds, $objects, $offset = null)
	{
		// Set offset to 0 if null

		if (is_null($offset) === true)
		{
			$offset = 0;
		}


		// Check if objects is an array

		if (is_array($objects) === false)
		{
			e
			(
				EXCEPTION_ARRAY_EXPECTED,
				array
				(
					'array expected' => $objects
				)
			);
		}

		$objectsHistorized = array();


		// List all objects needed and stores them

		foreach ($objects as $extensionId => $classNames)
		{
			// Get the name space

			$namespace = str_replace('-', '\\', $extensionId);

			// Get path to extension

			$pathToExtension = app()->getPathHelper()->convertExtensionIdToPath($extensionId) . 'Model/';

			$models = app()->getModelManager()->getModels($namespace, $pathToExtension);

			//

			foreach ($models as $model)
			{
				// Find all objects for a model
				
				if (in_array($model, $classNames) === true)
				{
					// Does the model exist ?

					$pathToModel = $pathToExtension . $model . '.php';

					if (file_exists($pathToModel) == false)
					{
						e
						(
							EXCEPTION_FILE_NOT_FOUND,
							array
							(
								'pathToFile' => $pathToModel
							)
						);
					}


					// Good, create the object

					require_once($pathToModel);

					$className = $namespace . '\\' . $model;

					$object = new $className();

					
					if ($offset > 0)
					{
						$offset = ($offset * pref('[Loop]/ui/nbItemsPerPage')) + 1;
					}

					$options = array
					(
						'orderBy' => array('timeOfCreation'),
						'orderDirection' => 'DESC',
						'limit' => array($offset, pref('[Loop]/ui/nbItemsPerPage'))
					);

					if (is_array($userIds) === true)
					{
						$options['conditions'] = array
						(
							'userCreatorId' => array(IN, $userIds)
						);
					}


					$results = $object->find($options);

					foreach ($results as $id => $result)
					{
						$object = new $className();
						$object->loadFromRawObject($result);

						$results[$id]['className'] = $object;
					}

					$objectsHistorized = array_merge($objectsHistorized, $results);

				}	
			}
		}

		
		// Sort points descrement

		$nbObjectsHistorized = count($objectsHistorized);

		for ($i = 0; $i < $nbObjectsHistorized - 1; $i++)
		{
			for ($j = $nbObjectsHistorized - 1; $j > $i; $j--)
			{
				if ($objectsHistorized[$j]['timeOfCreation'] > $objectsHistorized[$j - 1]['timeOfCreation'])
				{
					$tmp = $objectsHistorized[$j];
					$objectsHistorized[$j] = $objectsHistorized[$j - 1];
					$objectsHistorized[$j - 1] = $tmp;
				}
			}
		}

		return $objectsHistorized;
	}
	
	
	/**
	 * Returns the shared object properties of a given object name
	 *
	 * @param 	string 	$objectName 	The name of the object that we want to get shared properties
	 *
	 * @return 	array 	The array which contains the shared object properties
	 */
	
	public function getSharedObject($objectName)
	{
		// Does the object exist?

		if (isset($this->sharedObjects[$objectName]) == false)
		{
			e
			(
				EXCEPTION_OBJECT_NOT_FOUND,
				array
				(
					'objectName' => $objectName
				)
			);
		}


		return $this->sharedObjects[$objectName];
	}
	
	
	/**
	 * Returns all the shared objects properties
	 *
	 * @return 	array 	The array which contains the shared objects properties
	 */
	
	public function getSharedObjects()
	{
		return $this->sharedObjects;
	}


	/**
	 * Sets the given object as shared object
	 *
	 * @param 	string 	$objectName 	The name of the object
	 * @param 	object 	$object 		The object passed by reference
	 */
	
	public function shareObject($objectName, &$object)
	{
		$this->sharedObjects[$objectName] = $object;
	}
}

?>
